﻿using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D11;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Engine.Graphics.Shaders
{
    class ShadowMapShader
    {
        Matrix orthoMatrix;
        private Engine engine;
        PixelShader ps;
        VertexShader vs;
        private SamplerStateDescription samplerStateDescriptionClamp;
        private SamplerStateDescription samplerStateDescriptionWrap;
        private SamplerState samplerClamp;
        private SamplerState samplerWrap;
        private Vector3 lightDirection;

        public void CreateOrthoMatrix(float width, float height)
        {
            float zNear = 1;
            float depth = 50;
            Matrix.OrthoLH(width, height, zNear, depth);
        }
        public ShadowMapShader(Engine engine)
        {
            this.ShadowmapHeight = 1024;
            this.ShadowmapWidth = 1024;

            this.engine = engine;
            this.samplerStateDescriptionClamp = new SharpDX.Direct3D11.SamplerStateDescription()
            {
                AddressU = SharpDX.Direct3D11.TextureAddressMode.Clamp,
                AddressV = SharpDX.Direct3D11.TextureAddressMode.Clamp,
                AddressW = SharpDX.Direct3D11.TextureAddressMode.Clamp,
                BorderColor = SharpDX.Color4.Black,
                ComparisonFunction = SharpDX.Direct3D11.Comparison.Always,
                Filter = SharpDX.Direct3D11.Filter.MinMagMipLinear,
                MaximumAnisotropy = 1,
                MaximumLod = float.MaxValue,
                MinimumLod = 0,
                MipLodBias = 1.0f
            };
            this.samplerStateDescriptionWrap = new SharpDX.Direct3D11.SamplerStateDescription()
            {
                AddressU = SharpDX.Direct3D11.TextureAddressMode.Wrap,
                AddressV = SharpDX.Direct3D11.TextureAddressMode.Wrap,
                AddressW = SharpDX.Direct3D11.TextureAddressMode.Wrap,
                BorderColor = SharpDX.Color4.Black,
                ComparisonFunction = SharpDX.Direct3D11.Comparison.Always,
                Filter = SharpDX.Direct3D11.Filter.MinMagMipLinear,
                MaximumAnisotropy = 1,
                MaximumLod = float.MaxValue,
                MinimumLod = 0,
                MipLodBias = 1.0f
            };

            this.samplerClamp = new SharpDX.Direct3D11.SamplerState(this.engine.Grafic.Device, samplerStateDescriptionClamp);
            this.samplerWrap = new SharpDX.Direct3D11.SamplerState(this.engine.Grafic.Device, samplerStateDescriptionClamp);

            this.vs = new VertexShader(this.engine.Grafic.Device, ShaderBytecode.FromFile(@".\Shaders\ShadowmapVS.cso").Data);
            this.ps = new PixelShader(this.engine.Grafic.Device, ShaderBytecode.FromFile(@".\Shaders\ShadowmapPS.cso").Data);
        }

        public int ShadowmapWidth { get; set; }
        public int ShadowmapHeight { get; set; }
        public void SetLightDirection(Vector3 lightDirection)
        {
            this.lightDirection = lightDirection;
        }
    }
}
